#include "../../config.h"

#include <glibmm/i18n.h>

#include "../application.h"
#include "../mainwindow.h"
#include "p_channellist.h"
#include "p_macros.h"

namespace gpyp {
namespace pref {

//========================
// ColumnList
void ColumnList::enable_on_cell_data(Gtk::CellRenderer   *cell,
                                     const Gtk::TreeIter &iter) {
  dynamic_cast<Gtk::CellRendererToggle *>(cell)
    ->set_active(*get_item(iter)->enable());
}
void ColumnList::title_on_cell_data(Gtk::CellRenderer   *cell,
                                    const Gtk::TreeIter &iter) {
  dynamic_cast<Gtk::CellRendererText *>(cell)
    ->property_text() = *get_item(iter)->title();
}
void ColumnList::format_on_cell_data(Gtk::CellRenderer   *cell,
                                     const Gtk::TreeIter &iter) {
  dynamic_cast<Gtk::CellRendererText *>(cell)
    ->property_text() = *get_item(iter)->format();
}
void ColumnList::right_on_cell_data(Gtk::CellRenderer   *cell,
                                    const Gtk::TreeIter &iter) {
  dynamic_cast<Gtk::CellRendererToggle *>(cell)
    ->set_active(*get_item(iter)->right());
}
void ColumnList::sortasc_on_cell_data(Gtk::CellRenderer   *cell,
                                      const Gtk::TreeIter &iter) {
  dynamic_cast<Gtk::CellRendererToggle *>(cell)
    ->set_active(*get_item(iter)->sortasc());
}
void ColumnList::expand_on_cell_data(Gtk::CellRenderer   *cell,
                                     const Gtk::TreeIter &iter) {
  dynamic_cast<Gtk::CellRendererToggle *>(cell)
    ->set_active(*get_item(iter)->expand());
}
void ColumnList::ellipsize_on_cell_data(Gtk::CellRenderer   *cell,
                                        const Gtk::TreeIter &iter) {
  dynamic_cast<Gtk::CellRendererToggle *>(cell)
    ->set_active(*get_item(iter)->ellipsize());
}

void ColumnList::enable_on_toggled(const Glib::ustring &path) {
  RefChannelListColumn c = get_item(path);
  c->enable() = !*c->enable();
}
void ColumnList::title_on_edited(const Glib::ustring &path,
                                 const Glib::ustring &new_text) {
  get_item(path)->title() = new_text;
}
void ColumnList::format_on_edited(const Glib::ustring &path,
                                  const Glib::ustring &new_text) {
  get_item(path)->format()->set_value(new_text);
}
void ColumnList::right_on_toggled(const Glib::ustring &path) {
  RefChannelListColumn c = get_item(path);
  c->right() = !*c->right();
}
void ColumnList::sortasc_on_toggled(const Glib::ustring &path) {
  RefChannelListColumn c = get_item(path);
  c->sortasc() = !*c->sortasc();
}
void ColumnList::expand_on_toggled(const Glib::ustring &path) {
  RefChannelListColumn c = get_item(path);
  c->expand() = !*c->expand();
}
void ColumnList::ellipsize_on_toggled(const Glib::ustring &path) {
  RefChannelListColumn c = get_item(path);
  c->ellipsize() = !*c->ellipsize();
}

ColumnList::ColumnList(Application &app, Gtk::Window &/*parent*/) :
  ListView<gpyp::ChannelList>(app.mainwindow()->channellist()),
  column_enable_   ("",                renderer_enable_),
  column_title_    (_("Title"),        renderer_title_),
  column_format_   (_("Format"),       renderer_format_),
  column_right_    (_("Right"),        renderer_right_),
  column_sortasc_  (_("Sort asc"),     renderer_sortasc_),
  column_expand_   (_("Expand"),       renderer_expand_),
  column_ellipsize_(_("Ellipsize"),    renderer_ellipsize_)
{
  renderer_enable_.signal_toggled()
    .connect(sigc::mem_fun(*this, &ColumnList::enable_on_toggled));
  renderer_title_.property_editable() = true;
  renderer_title_.signal_edited()
    .connect(sigc::mem_fun(*this, &ColumnList::title_on_edited));
  renderer_format_.property_editable() = true;
  renderer_format_.property_ellipsize() = Pango::ELLIPSIZE_END;
  renderer_format_.signal_edited()
    .connect(sigc::mem_fun(*this, &ColumnList::format_on_edited));
  renderer_right_.signal_toggled()
    .connect(sigc::mem_fun(*this, &ColumnList::right_on_toggled));
  renderer_sortasc_.signal_toggled()
    .connect(sigc::mem_fun(*this, &ColumnList::sortasc_on_toggled));
  renderer_expand_.signal_toggled()
    .connect(sigc::mem_fun(*this, &ColumnList::expand_on_toggled));
  renderer_ellipsize_.signal_toggled()
    .connect(sigc::mem_fun(*this, &ColumnList::ellipsize_on_toggled));

  column_format_.set_expand(true);
#define CELL_DATA(NAME)                                                 \
  column_##NAME##_.set_cell_data_func(renderer_##NAME##_, sigc::mem_fun(*this, &ColumnList::NAME##_on_cell_data))
  CELL_DATA(enable);
  CELL_DATA(title);
  CELL_DATA(format);
  CELL_DATA(right);
  CELL_DATA(sortasc);
  CELL_DATA(expand);
  CELL_DATA(ellipsize);
#undef CELL_DATA

  append_column(column_enable_);
  append_column(column_title_);
  append_column(column_format_);
  append_column(column_right_);
  append_column(column_sortasc_);
  append_column(column_expand_);
  append_column(column_ellipsize_);
}

//========================
// ChannelList
ChannelList::ChannelList(Application &app, Gtk::Window &parent) :
  Gtk::VBox  (false, 2),
  app_       (app),
  sort_color_(0, 2),
  list_      (app, parent)
{
  SETUP_SPINBUTTON(sort_color_, -1, 1);
  sort_color_.set_increments(0.02, 0.1);

  Gtk::Table *t1 = Gtk::manage(new Gtk::Table());
  TABLE_LABEL(*t1, 0, _("Sort column brightness"));
  t1->attach(sort_color_, 1, 2, 0, 1);
  t1->set_col_spacings(2);
  t1->set_row_spacings(2);
  pack_start(*t1, false, false);

  Gtk::ScrolledWindow *sw1 = Gtk::manage(new Gtk::ScrolledWindow());
  sw1->add(list_);
  sw1->set_shadow_type(Gtk::SHADOW_IN);
  sw1->set_policy(Gtk::POLICY_AUTOMATIC, Gtk::POLICY_AUTOMATIC);
  Gtk::Button *b_u = Gtk::manage(new Gtk::Button(Gtk::Stock::GO_UP));
  Gtk::Button *b_d = Gtk::manage(new Gtk::Button(Gtk::Stock::GO_DOWN));
  Gtk::Button *b_a = Gtk::manage(new Gtk::Button(Gtk::Stock::ADD));
  Gtk::Button *b_r = Gtk::manage(new Gtk::Button(Gtk::Stock::REMOVE));
  b_u->signal_clicked().connect(sigc::mem_fun(list_, &ColumnList::up));
  b_d->signal_clicked().connect(sigc::mem_fun(list_, &ColumnList::down));
  b_a->signal_clicked().connect(sigc::mem_fun(list_, &ColumnList::add));
  b_r->signal_clicked().connect(sigc::mem_fun(list_, &ColumnList::remove));
  Gtk::HButtonBox *h1 =
    Gtk::manage(new Gtk::HButtonBox(Gtk::BUTTONBOX_END, 2));
  h1->pack_start(*b_u, false, false);
  h1->pack_start(*b_d, false, false);
  h1->pack_start(*b_a, false, false);
  h1->pack_start(*b_r, false, false);
  Gtk::VBox *v1 = Gtk::manage(new Gtk::VBox(false, 2));
  v1->pack_start(*sw1);
  v1->pack_start(*h1, false, false);
  v1->set_border_width(2);
  Gtk::Frame *f2 = Gtk::manage(new Gtk::Frame(_("Column")));
  f2->add(*v1);
  pack_start(*f2);

  app_.mainwindow()->channellist()->sort_color()
    ->attach(sort_color_.property_value());
}

}/*pref*/
}/*gpyp*/
